package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院/科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String key = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    @PostMapping("/schedule/list")
    public Result findSche(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String key = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo vo = new ScheduleQueryVo();
        vo.setHoscode(hoscode);
        vo.setDepcode(depcode);
        Page<Schedule> pages = scheduleService.findPageSchedule(page, limit, vo);

        return Result.ok(pages);


    }

    @PostMapping("saveSchedule")
    public Result save(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院/科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String key = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    // 删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartmetn(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院/科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String key = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();

    }

    @PostMapping("department/list")
    public Result finddep(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String key = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo vo = new DepartmentQueryVo();
        vo.setHoscode(hoscode);
        Page<Department> pages = departmentService.findPageDepartment(page, limit, vo);


        return Result.ok(pages);
    }

    @PostMapping("/saveDepartment")
    public Result saveDep(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");

        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String key = hospitalSetService.getSignKey(hoscode);

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);
        return Result.ok();

    }

    /**
     * 查询医院的接口
     *
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 获取传入参数
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String key = hospitalSetService.getSignKey(hoscode);

        // 比对签名
        String hospSign = (String) paramMap.get("sign");

        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }


    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 比对签名
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String key = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(key);
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        hospitalService.save(paramMap);
        return Result.ok();
    }


}
